package com.atguigu.tingshu.order.strategy.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;

@Service
@Slf4j
public  class AlbumOrderTradeStrategy implements OrderTradeStrategy {


    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private RedisTemplate<String , String> redisTemplate ;
    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
        log.info("AlbumOrderTradeStrategy....trade方法执行了....");
        //如果是购买专辑 经过接口的策越设计模式 跳转我album的实现类
        OrderInfoVo orderInfoVo = new OrderInfoVo();

        //根据id获取专辑数据  AlbumInfo 是数据库的数据 UserInfoVo是我们自定义的实体类
        Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(tradeVo.getItemId());
        AlbumInfo albumInfo = albumInfoResult.getData();

        //根据id获取用户信息
        Long userId = AuthContextHolder.getUserId(); //判断是否为会员
        Result<UserInfoVo>  userInfoVoResult = userInfoFeignClient.findUserInfoVoByUserId(userId);
        UserInfoVo userInfoVo = userInfoVoResult.getData();

        //金额数据的构建
        BigDecimal originalAmount = albumInfo.getPrice();

        if(userInfoVo.getIsVip().equals("1")&& new Date().before(userInfoVo.getVipExpireTime())){ //用户为vip并且没有过期
            //按照vip的减免进行减免金额
            BigDecimal vipDiscount = albumInfo.getVipDiscount();
            if (vipDiscount.intValue()!=-1){
                BigDecimal orderAmount = originalAmount.multiply(vipDiscount)
                        .multiply(new BigDecimal("10"))
                        .divide(new BigDecimal("10"),2,BigDecimal.ROUND_HALF_UP);

                orderInfoVo.setOriginalAmount(originalAmount);
            }
        }else{
            BigDecimal discount = albumInfo.getDiscount();
            if(discount.intValue()!=-1){
                BigDecimal orderAmount = originalAmount.multiply(discount)
                        .multiply(new BigDecimal("10"))
                        .divide(new BigDecimal("10"),2,BigDecimal.ROUND_HALF_UP);

                orderInfoVo.setOriginalAmount(originalAmount);
            }else {
                orderInfoVo.setOrderAmount(originalAmount);
            }
        }

        orderInfoVo.setDerateAmount(originalAmount.subtract(orderInfoVo.getOrderAmount()));

        //订单明细数据
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(tradeVo.getItemId());
        orderDetailVo.setItemName(albumInfo.getAlbumTitle());
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderDetailVo.setItemPrice(orderInfoVo.getOrderAmount());
        orderInfoVo.setOrderDetailVoList(Arrays.asList(orderDetailVo));


        //减免明细数据
        if(0 != orderInfoVo.getDerateAmount().intValue()) {
            OrderDerateVo orderDerateVo = new OrderDerateVo() ;
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(orderInfoVo.getDerateAmount());
            orderDerateVo.setRemarks("专辑购买折扣");
            orderInfoVo.setOrderDerateVoList(Arrays.asList(orderDerateVo));
        }

        // 给OrderInfoVo设置其他的数据(交易编号、付款项目类型、支付方式、时间戳、签名)
        this.setOtherInfo(orderInfoVo , tradeVo, redisTemplate);

        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_ALBUM;
    }
}
